package com.pengshun.modules.Resolver;

import com.pengshun.modules.domain.User;
import com.pengshun.modules.security.CurrUserInfo;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

@Component
public class CurrEmployeeMethodArgumentResolver implements HandlerMethodArgumentResolver {


    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        if (methodParameter.getParameterType().isAssignableFrom(User.class) &&
                methodParameter.hasParameterAnnotation(CurrUserInfo.class)) {
            return true;
        }
        return false;
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        String id=nativeWebRequest.getAttribute("uiId", RequestAttributes.SCOPE_REQUEST).toString();
        String name=nativeWebRequest.getAttribute("name", RequestAttributes.SCOPE_REQUEST).toString();
        String phone=nativeWebRequest.getAttribute("phone", RequestAttributes.SCOPE_REQUEST).toString();
        User user= new User(Long .parseLong(id),name,phone);
        return user;
    }

}
